// pages/search/search.js
const http=require("../../http/http");
var timeutil=require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hotsongData:[],
    recommodsong:[],
    array: ['歌曲', '歌单', '专辑'],
    index: 0,
    keyword: '',
    searchSuccess: false,
    ShowMore: false,
    records:[],
    hotsongData: [],
    colors: [],
    searchSuccess:false
  },
  todayHottext(e){
    console.log(e.target.dataset.text)
    let keyword = e.target.dataset.text
    const res333= http.searchMusicsByKeywords({
      keyword: keyword,
      limit: 10,
      offset:0
    })
    res333.then((data)=>{
      console.log(data)
      let recordsLength = data.records.length
      let records = data.records
      if(recordsLength>0){
        this.setData({
          ShowMore:false,
          searchSuccess: true,
          records: records
        })
      }
    })
  },
  clickOnHottext(e){
    console.log(e.target.dataset.text)
    let keyword = e.target.dataset.text
    const res111= http.searchMusicsByKeywords({
      keyword: keyword,
      limit: 10,
      offset:0
    })
    res111.then((data)=>{
      console.log(data)
      let recordsLength = data.records.length
      let records = data.records
      if(recordsLength>0){
        this.setData({
          searchSuccess:true,
          records: records
        })
      }
    })
  },
  clickOnRecommodSong(e){
    console.log(e.target.dataset.text)
    let keyword = e.target.dataset.text
    const res222= http.searchMusicsByKeywords({
      keyword: keyword,
      limit: 10,
      offset:0
    })
    res222.then((data)=>{
      console.log(data)
      let recordsLength = data.records.length
      let records = data.records
      if(recordsLength>0){
        this.setData({
          searchSuccess:true,
          records: records
        })
      }
    })
  },
  bindPickerChange: function (e) {
    let choose = e.detail.value
    if(choose==0){
      console.log(choose)
      this.setData({
        index: choose
      })
    }else if(choose==1){
      console.log(choose)

      this.setData({
        index: choose
      })
    }else{
      console.log(choose)

      this.setData({
        index: choose
      })
    }
  },
  focusOnInput(){
    this.setData({
      searchSuccess: false,
      ShowMore: false
    })
    // this.onLoad();
  },
  bindInput: function (e) {
    console.log(e.detail.value)
    this.setData({
      keyword: e.detail.value,
      searchSuccess: false
    })
    // wx.redirectTo({
    //   url: '/pages/search/search',
    // })
  },

  bindSearch: function () {
    let choose = this.data.index
    let keyword = this.data.keyword
    if(''===keyword){
      wx.showToast({
        icon: 'none',
        title: '请输入关键字',
        duration: 1000,
      })
    }

    if(choose==0){
      console.log(choose)
      const res11= http.searchMusicsByKeywords({
        keyword: keyword,
        limit: 10,
        offset:0
      })
      res11.then((data)=>{
        console.log(data)
        let recordsLength = data.records.length
        let records = data.records
        if(recordsLength>0){
          this.setData({
            searchSuccess:true,
            records: records
          })
        }
      })
    }else if(choose==1){
      console.log(choose)
      const res22= http.searchMusicListByKeywords({
        keyword: keyword,
        limit: 10,
        offset:0
      })
      res22.then((data)=>{
        console.log(data)
        let recordsLength = data.records.length
        let records = data.records
        if(recordsLength>0){
          this.setData({
            searchSuccess:true,
            records: records
          })
        }
      })
    }else{
      console.log(choose)
      const res33= http.searchMusicsAlbumsByKeywords({
        keyword: keyword,
        limit: 10,
        offset:0
      })
      res33.then((data)=>{
        console.log(data)
        let recordsLength = data.records.length
        let records = data.records
        if(recordsLength>0){
          // 格式化时间戳
          for(var i=0;i<records.length;i++){
            records[i].publishTime = timeutil.formatTimeTwo(records[i].publishTime/1000,'Y-M')
          }
          this.setData({
            searchSuccess:true,
            records: records
          })
        }
      })
    }

  },


  /** 
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      ShowMore: false,
    })
    const res1= http.gethottext({
      limit:5 
    })
    res1.then((data)=>{
      console.log(data)
      this.setData({
        hotsongData:data
      })
    })
    const res2=http.recommodsong({
      limit:10
    })
    res2.then((data)=>{
      console.log(data)
      this.setData({
        recommodsong:data
      })
    })
    
  },
  getmorehotsong:function(e){
    const res3 = http.gethottext({
      limit: 50
    })
    res3.then((data) => {
      console.log(data)
      this.setData({
        hotsongData: data,
      })
    });
    var color = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F'];
    var colors = [];
    for (var i = 0; i < 50; i++) {
      var color_code = '#';
      for (var j = 0; j < 6; j++) {
        color_code += color[Math.floor(Math.random() * 16)];
      }
      colors.push(color_code);
    }
    // console.log(colors);
    this.setData({
      colors:colors
    })
    this.setData({
      ShowMore: true
    })
    // wx.navigateTo({
    //   url: '/pages/morehotsong/morehotsong',
    // })
  },
  onSwitch1Change:function({detail}){
    console.log(detail)
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})